package com.ham.mud.commands;

import com.ham.mud.ServerConnection;
import com.ham.mud.items.Item;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by hlucas on Jun 21, 2011 at 3:03:14 PM
 */
public class InventoryCommand extends PlayerCommand {
    @Override
    public void execute(ServerConnection connection, String[] params) {
        connection.printStart("Your inventory:");
        List<Item> inventory = connection.getPlayer().getInventory();
        Map<String, Integer> counts = new HashMap<String, Integer>();
        Map<String, Item> items = new HashMap<String, Item>();
        for(Item item : inventory) {
            Integer count = counts.get(item.getFileName());
            if(count == null) count = 0;
            count++;
            counts.put(item.getFileName(), count);
            items.put(item.getFileName(), item);
        }

        Set<String> keys = counts.keySet();
        for(String key : keys) {
            Integer count = counts.get(key);
            Item item = items.get(key);
            if(count == 1) {
                connection.println("      " + item.getName() + item.getAffects());
            } else {
                if(count < 10) {
                    connection.println("  (" + count + ") " + item.getName() + item.getAffects());
                } else {
                    connection.println(" (" + count + ") " + item.getName() + item.getAffects());
                }
            }
        }

        connection.printEnd();
    }

    @Override
    protected String getCommandName() {
        return "inventory";
    }
}
